/* Copyright(c) 2010- Siro-info Co.(http://www.siro-info.com)
 * All rights reserved.
 */
package com.siro.pm.ui.view;

import com.siro.pm.I18nManager;
import com.siro.pm.PmApplication;
import com.siro.pm.model.DemandOrBug;
import com.siro.pm.model.Developer;
import com.siro.pm.model.Project;
import com.siro.pm.model.provider.DemandOrBugProvider;
import com.siro.pm.model.provider.ProjectProvider;
import com.siro.pm.ui.detail.BugDetailPage;
import com.siro.pm.ui.main.MainPage;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.HorizontalSplitPanel;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

/**
 * Class Declaration:
 * 
 * @version v1.0
 * @since UserView.java v1.0
 */
public class UserView extends HorizontalSplitPanel {

	private static final long serialVersionUID = 9015647817936419772L;

	private MainPage mainPage;
	private Table projectsTable;
	private Table demandOrBugsTable;

	public UserView() {
		setSizeFull();
		setHeight("100%");
		setSplitPosition(60, Sizeable.UNITS_PERCENTAGE);

		VerticalLayout bugsLayout = new VerticalLayout();
		bugsLayout.setSizeFull();
		bugsLayout.setHeight("100%");
		bugsLayout.setSpacing(true);
		bugsLayout.setMargin(true, false, false, true);

		// demandOrBug table
		demandOrBugsTable = new Table();
		demandOrBugsTable.setStyleName("h2");
		demandOrBugsTable.setCaption("  "
				+ I18nManager.getMessage("userview.bugs.caption"));
		demandOrBugsTable.setHeight("100%");
		demandOrBugsTable.setWidth("100%");
		demandOrBugsTable.setSelectable(true);
		demandOrBugsTable.setImmediate(true);
		demandOrBugsTable.setColumnWidth(DemandOrBugProvider.BUG_PROPERTY_NAME,
				130);
		demandOrBugsTable.setColumnWidth(
				DemandOrBugProvider.BUG_PROPERTY_PROJECT, 100);
		demandOrBugsTable.setContainerDataSource(DemandOrBugProvider
				.getBugsData());
		// turn on column reordering and collapsing
		demandOrBugsTable.setColumnReorderingAllowed(true);
		demandOrBugsTable.setColumnCollapsingAllowed(true);
		demandOrBugsTable.addListener(new Table.ValueChangeListener() {
			private static final long serialVersionUID = 3461107729227091339L;

			public void valueChange(ValueChangeEvent event) {
				Object value = event.getProperty().getValue();
				BugDetailPage bugDetailPage = new BugDetailPage();
				bugDetailPage.setBug((DemandOrBug) value);
				bugDetailPage.createContents();
				Window bugDetailWindow = new Window(I18nManager
						.getMessage("bug.info"));
				bugDetailWindow.setModal(true);
				bugDetailWindow.setWidth("800px");
				bugDetailWindow.setHeight("80%");
				bugDetailPage.setWindow(bugDetailWindow);
				bugDetailWindow.addComponent(bugDetailPage);
				getWindow().addWindow(bugDetailWindow);
			}
		});

		bugsLayout.addComponent(demandOrBugsTable);
		bugsLayout.setExpandRatio(demandOrBugsTable, 1);
		setFirstComponent(bugsLayout);

		VerticalLayout projectLayout = new VerticalLayout();
		projectLayout.setSizeFull();
		projectLayout.setHeight("100%");
		projectLayout.setSpacing(true);
		projectLayout.setMargin(true, false, false, true);
		// projects table
		projectsTable = new Table();
		projectsTable.setStyleName("h2");
		projectsTable.setCaption("  "
				+ I18nManager.getMessage("userview.project.caption"));
		projectsTable.setHeight("100%");
		projectsTable.setWidth("100%");
		projectsTable.setSelectable(true);
		projectsTable.setImmediate(true);
		projectsTable.setColumnWidth(
				ProjectProvider.PROJECT_PROPERTY_PROJECTNAME, 100);
		// projectsTable.setColumnWidth(
		// ProjectProvider.PROJECT_PROPERTY_PROJECTMANAGER, 100);
		// projectsTable.setColumnWidth(ProjectProvider.PROJECT_PROPERTY_STATUS,
		// 100);
		// projectsTable.setColumnWidth(
		// ProjectProvider.PROJECT_PROPERTY_STARTTIME, 100);
		projectsTable.setContainerDataSource(ProjectProvider
				.getProjectShortCutData((Developer) PmApplication.getDefault()
						.getUser()));
		// turn on column reordering and collapsing
		projectsTable.setColumnReorderingAllowed(true);
		projectsTable.setColumnCollapsingAllowed(true);
		projectsTable.addListener(new Table.ValueChangeListener() {
			private static final long serialVersionUID = 3461107729227091339L;

			public void valueChange(ValueChangeEvent event) {
				Object value = event.getProperty().getValue();
				ProjectView projectView = new ProjectView();
				projectView.setUserSelectProject((Project) value);
				projectView.createContents();
				mainPage.setCentralContent(projectView);
			}
		});

		projectLayout.addComponent(projectsTable);
		projectLayout.setExpandRatio(projectsTable, 1);
		setSecondComponent(projectLayout);
	}

	public MainPage getMainPage() {
		return mainPage;
	}

	public void setMainPage(MainPage mainPage) {
		this.mainPage = mainPage;
	}

}
